package com.xquant.common.engine.xPP.cxf;

import com.xquant.common.engine.xPP.cxf.webservice.*;
import com.xquant.common.engine.xPP.cxf.webservice.*;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class XQuantPPStubFactory {

    private Lock lock = new ReentrantLock();

    private Map pp;

    private XQuantPPStub xQuantPPStub;

    public Map getPp() {
        return pp;
    }

    public XQuantPPStub getxQuantPPStub() {
        return xQuantPPStub;
    }

    public XQuantPPStubFactory(Map pp) {
      /*int connectionTimeout = Integer.parseInt(ConfigSupport.getInstance().getProperty("cxfClient.connectionTimeout"));
        int receiveTimeout = Integer.parseInt(ConfigSupport.getInstance().getProperty("cxfClient.receiveTimeout"));*/
        int connectionTimeout = 60000;
        int receiveTimeout = 720000000;
        this.pp = pp;
        xQuantPPStub = new XQuantPPStub(MapUtils.getString(pp, "address"), MapUtils.getIntValue(pp, "threads"));
        xQuantPPStub.setConnectionParams(connectionTimeout, receiveTimeout);
    }

    public Lock getLock(){
        return lock;
    }

    public Integer getStatus(){
        lock.lock();
        try{
            return Integer.parseInt(MapUtils.getString(pp,"status"));
        }finally {
            lock.unlock();
        }
    }

    public void setStatus(int status) {
        lock.lock();
        try{
            pp.put("status", status);
        }finally {
            lock.unlock();
        }
    }

    public boolean testMethod(XQuantPPStubFactory factory) {
        try {
            this.xQuantPPStub.testMethod(factory,new TestMethod()).getTestMethodResult();
            setStatus(0);
            uploadDll();
            return true;
        } catch (Exception ex) {
            //ex.printStackTrace();
            setStatus(-1);
            return false;
        }
    }

    public void restart() {
        this.xQuantPPStub.restart(new Restart()).getRestartResult();
    }

    public void uploadDll(){
        if(pp.get("uploadDllNames")==null || ((List)pp.get("uploadDllNames")).size() == 0){
            return;
        }
        Iterator iterator = ((List<String>) pp.get("uploadDllNames")).iterator();
        while(iterator.hasNext()){
            try {
                String filePath = (String)iterator.next();
                File file = new File(filePath);
                String newFilepath = MapUtils.getString(pp, "dllPath") + "\\" + file.getName();
                File newfile = new File(newFilepath);
                FileUtils.copyFile(file,newfile);
                iterator.remove();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public NotifyDataResponse notifyData(NotifyParam param) {
        NotifyData notifyData = new NotifyData();
        notifyData.setParam(param);
        NotifyDataResponse response = this.xQuantPPStub.notifyData(notifyData);
//        System.out.println(response.getNotifyDataResult().getErrorCode()+"----"+response.getNotifyDataResult().getErrorMsg());
        return response;
    }
}
